﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using exsportance.Silverlight.Common.Utils;
using System.Collections.Generic;

namespace exsportance.Silverlight.Validate.Validators
{
    /// <summary>
    /// Validator that returns true if value is valid email address
    /// </summary>
    public class EmailAddressValidator : BaseValidator
    {
        #region members
        /// <summary>
        /// Validation failure message key for when the value is no valid email
        /// </summary>
        private const string NOT_EMAIL = "notEmail";

        /// <summary>
        /// Validation failure message key for when the value is an empty string
        /// </summary>
        private const string STRING_EMPTY = "stringEmpty";

        protected Dictionary<string, string> _messagesTemplates = new Dictionary<string, string>(){
        {EmailAddressValidator.NOT_EMAIL,"'%value%' is not a valid email address in the basic format local-part@hostname"},
        {EmailAddressValidator.STRING_EMPTY,"'%value%' is an empty string"},
        };
        #endregion

        #region protected methods
        protected override bool CheckValid(object value)
        {
            string valueString = value as string;

            rememberValue(valueString);

            if (valueString.IsNullOrEmpty())
            {
                raiseMessage(EmailAddressValidator.STRING_EMPTY);
                return false;
            }

            // check
            if (!valueString.IsEmail())
            {
                raiseMessage(EmailAddressValidator.NOT_EMAIL);
                return false;
            }

            return true;
        }

        protected override System.Collections.Generic.Dictionary<string, string> getMessageTemplates()
        {
            return _messagesTemplates;
        }
        #endregion
    }
}
